package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.AuditStatusEnum;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditorMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAuditor;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditorService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {


    @Autowired
    AgencyCertificationAuditorMapper certificationAuditorMapper;



    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    @Override
    @Transactional
    public void add(AgencyCertificationAuditAddReqDTO reqDTO) {
        //判断是否第一次认证
        AgencyCertification one = lambdaQuery().eq(AgencyCertification::getId, UserContext.currentUserId()).one();

        if (ObjectUtil.isEmpty(one)) {
            //第一次认证，新增数据
            //添加认证表
            AgencyCertification agencyCertification = BeanUtils.copyBean(reqDTO, AgencyCertification.class);
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertification.setId(UserContext.currentUserId());
            int flag1 = baseMapper.insert(agencyCertification);

            //添加审核表
            AgencyCertificationAuditor agencyCertificationAuditor = new AgencyCertificationAuditor();
            agencyCertificationAuditor.setId(UserContext.currentUserId());
            agencyCertificationAuditor.setAuditStatus(AuditStatusEnum.UNREVIEWED.getStatus());
            int flag2 = certificationAuditorMapper.insert(agencyCertificationAuditor);

            if (flag1 != 1 || flag2 != 1) {
                throw new CommonException("提交认证失败");
            }
        } else {
            //重新认证
            if (!one.getCertificationStatus().equals(CertificationStatusEnum.FAIL.getStatus())) {
                throw new CommonException("重新认证失败，状态不正确");
            }
            //修改认证表
            AgencyCertification agencyCertification = BeanUtils.copyBean(reqDTO, AgencyCertification.class);
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertification.setId(UserContext.currentUserId());
            int i = baseMapper.updateById(agencyCertification);
            //修改审核表
            AgencyCertificationAuditor agencyCertificationAuditor = new AgencyCertificationAuditor();
            agencyCertificationAuditor.setAuditStatus(AuditStatusEnum.UNREVIEWED.getStatus());
            agencyCertificationAuditor.setId(UserContext.currentUserId());
            certificationAuditorMapper.updateById(agencyCertificationAuditor);


        }

    }


}
